package com.maxh.controller;

import com.maxh.annotation.SuperAuthority;
import com.maxh.annotation.AdminLoginToken;
import com.maxh.annotation.UserLoginToken;
import com.maxh.service.TokenService;
import com.maxh.service.UserService;
import com.maxh.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.alibaba.fastjson.JSONObject;
import com.maxh.pojo.User;

import java.util.List;
import java.util.Map;



@RestController
public class UserController {
	@Autowired
	UserService userService;
	@Autowired
	TokenService tokenService;


	/*获取用户信息*/
	@AdminLoginToken
	@GetMapping("/training/user/student/list")
	public Object userList(){
		List userList = (List) userService.userList();
		System.out.println("用户信息："+userList);
		if(userList.size()>0){
			return new ReturnData(StatusCode.REQUEST_SUCCESS,userList ,"后端数据交互正常！");
		}
		return new ReturnData(StatusCode.REQUEST_SUCCESS,userList ,"数据为空！");
	}

	/*添加用户*/
	@PostMapping("/training/user/student/add")
	public Object addUser(@RequestParam Map<String,Object> map){
		if(map.get("userNum")==null||map.get("username")==null||map.get("phone")==null
				||map.get("email")==null||map.get("sex")==null||map.get("birthday")==null){
			return new ReturnError(StatusCode.RETURN_ERROR,"必填数据不能为空");
		}
		/*获取当前时间*/
		map.put("status",1);
		String createTime = new ReturnUtil().nowTime();
		map.put("createTime",createTime);
		String oldPassMD5 = MD5Util.getMD5("12345678");
		map.put("password",oldPassMD5);
		System.out.println("添加用户信息："+map);
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("findUserNum", map.get("userNum"));
		List userList =  userService.searchUser(jsonObject);
		System.out.println(jsonObject);
		if(userList.size()>0)
			return new ReturnError(StatusCode.RETURN_ERROR,"账号已存在，请重新输入");
		Boolean isAdd = userService.addUser(map);
		System.out.println(isAdd);
        List user =  userService.searchUser(jsonObject);
		if(isAdd){
			return new ReturnData(StatusCode.REQUEST_SUCCESS, user,"注册成功！");
		}else{
			return new ReturnError(StatusCode.RETURN_ERROR,"注册失败");
		}
	}

	/*管理员修改用户*/
	@AdminLoginToken
	@PostMapping("/training/user/student/update")
	public Object updateUser(@RequestParam Map<String,Object> map){
		if(map.get("id")==null||map==null){
			return new ReturnError(StatusCode.RETURN_ERROR,"Id不能为空");
		};
		System.out.println("更新用户信息："+map);
		Boolean isAdd = userService.updateUser(map);
		if(isAdd){
			List userList =  userService.searchUser(map);
			System.out.println(userList);
			return new ReturnData(StatusCode.REQUEST_SUCCESS, userList,"修改成功！");
		}else{
			return new ReturnError(StatusCode.RETURN_ERROR,"修改用户信息失败");
		}
	}

    /*删除用户*/
	@SuperAuthority
    @PostMapping("/training/user/student/delete")
    public Object deleteUser(@RequestParam Map<String,Object> map){
		System.out.println(map.get("id"));
		if(map.get("id")==null||map==null){
			return new ReturnError(StatusCode.RETURN_ERROR,"Id不能为空");
		}
        Boolean isDelete = userService.deleteUser(map.get("id")+"");
        if(isDelete){
            return new ReturnData(StatusCode.REQUEST_SUCCESS,"删除成功！");
        }else{
			return new ReturnError(StatusCode.RETURN_ERROR,"删除用户信息失败");
		}
    }

	/*根据用户姓名或学号查询用户信息*/
	@AdminLoginToken
	@GetMapping("/training/user/search")
	public Object searchUser(@RequestParam Map<String,Object> map){
		System.out.println(map);
		if(map.get("username")==null||map.get("userNum")==null){
			return new ReturnError(StatusCode.RETURN_ERROR,"搜索关键字不能为空");
		}
		List userList =  userService.searchUser(map);
		System.out.println("根据用户名查询到的用户信息："+userList);
		return new ReturnData(StatusCode.REQUEST_SUCCESS,userList ,"数据获取成功！");
	}

    /*学员修改信息*/
    @UserLoginToken
    @PostMapping("/training/user/student/info/update")
    public Object updateUserInfo(@RequestParam Map<String,Object> map){
		if(map==null){
			return new ReturnError(StatusCode.RETURN_ERROR,"修改信息不能为空");
		}
        System.out.println("更新用户信息："+map);
        String id = TokenUtil.getTokenUserId();
        map.put("id",id);
        Boolean isAdd = userService.updateUser(map);
        if(isAdd){
            List userList =  userService.searchUser(map);
            System.out.println(userList);
            return new ReturnData(StatusCode.REQUEST_SUCCESS, userList,"修改成功！");
        }else{
            return new ReturnError(StatusCode.RETURN_ERROR,"修改用户信息失败");
        }
    }

    /*学员获取个人信息*/
    @UserLoginToken
    @GetMapping("/training/user/student/info/list")
    public Object getUserInfo(){
        String id = TokenUtil.getTokenUserId();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("id", id);
        List userList =  userService.searchUser(jsonObject);
        if(userList.size()>0){
            System.out.println(userList);
            return new ReturnData(StatusCode.REQUEST_SUCCESS, userList,"数据获取成功！");
        }else{
            return new ReturnError(StatusCode.RETURN_ERROR,"修改用户信息失败");
        }
    }
}
